﻿
Public Class frmInicio
    Inherits frmMaster

    Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            txtRUTCliente.Attributes.Add("onkeypress", "return isNumber(event)")
            txtRUTCliente.Attributes.Add("onkeydown", "return isNumber(event)")
            txtDVRUTCliente.Attributes.Add("onkeydown", "return isAlphaNumber(event);")
            txtDVRUTCliente.Attributes.Add("onkeypress", "return isAlphaNumber(event);")
            txtDVRUTCliente.Attributes.Add("style", "text-transform: uppercase")
        End If
    End Sub

    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBuscar.Click
        If txtRUTCliente.Text.Trim = "" And txtDVRUTCliente.Text.Trim = "" Then
            MsgInformativo("Ingrese RUT.-")
        Else
            ' - Validar RUT
            If ValidaRut(txtRUTCliente.Text.Trim & "-" & txtDVRUTCliente.Text.Trim) Then
                If (SOAPGratis(txtRUTCliente.Text.Trim & "-" & txtDVRUTCliente.Text.Trim)) Then
                    MsgInformativo("RUT Cliente '" & txtRUTCliente.Text & "-" & txtDVRUTCliente.Text.ToUpper & "' ya tiene asociado un SOAP Gratis")

                    txtRUTCliente.Text = ""
                    txtRUTCliente.UpdateAfterCallBack = True
                    txtDVRUTCliente.Text = ""
                    txtDVRUTCliente.UpdateAfterCallBack = True
                Else
                    ConsultaDatosCliente(txtRUTCliente.Text)

                    If Not RUT Is Nothing Then
                        RUTCliente = txtRUTCliente.Text & "-" & txtDVRUTCliente.Text.ToUpper
                        'Email = "pablo.rodriguez@entersoft.cl" 'OJO - Sólo para desarrollo

                        IrA("frmListadoSeguros.aspx")
                    Else
                        MsgInformativo("Cliente Inexistente")
                    End If
                End If
            Else
                MsgInformativo("RUT No válido")
            End If
        End If
    End Sub

    Protected Sub ConsultaDatosCliente(ByVal RUT As String)
        Dim MiCLT As SRM.CLT0 = New SRM.CLT0("U062")

        ' - Consulta SCS por RUT y obtención de los datos personales (principales)
        If (MiCLT.ObtenerDatosCliente(RUT)) Then
            Me.RUT = RUT
            NombreCliente = MiCLT.Nombre
            ColorCliente = MiCLT.Color
            Email = ""
        End If
    End Sub

    Protected Function SOAPGratis(ByVal RUT As String) As Boolean
        Dim rp As New DotNetResponse.SQLPersistence
        Dim MiRespuesta As Boolean

        rp.Parametro = New String() {RUT}

        rp.StoredProcedure = "SP_GET_SOAP_GRATIS"

        ExecuteSP(rp, False, True)

        If Not rp.Errores Then
            If Not rp.DtTable Is Nothing Then
                MiRespuesta = rp.DtTable.Rows(0)(0)
            End If
        End If

        rp.Dispose()

        Return MiRespuesta
    End Function
End Class